package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.Comment;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.views.figures.CommentFigure;

public class CommentEditpart extends NodeTemplateEditpart {

	public CommentEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure createFigure() {
		return new CommentFigure();
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		Comment template = getComment();
		CommentFigure figure = (CommentFigure) getFigure();
		if (ModelPackage.LITERAL_CONTENT.equals(propertyName)) {
			figure.setText(template.getContent());
		} else if (ModelPackage.GRAPHICAL_DOMAIN_COLLAPSE.equals(propertyName)) {
			refreshSourceConnections();
			refreshTargetConnections();
		} else {
			super.handlePropertyChanged(event);
		}
	}

	@Override
	protected void refreshVisuals() {
		Comment template = getComment();
		CommentFigure figure = (CommentFigure) getFigure();
		super.refreshVisuals();
		figure.setText(template.getContent());
	}

	private Comment getComment() {
		return (Comment) getNode().getModelElement();
	}
	
	@Override
	protected List getModelSourceConnections() {
		if (getComment().getDomain() != null
				&& getComment().getDomain().isCollapsed()) {
			return Collections.EMPTY_LIST;
		} else {
			return super.getModelSourceConnections();
		}
	}

	@Override
	public List getModelTargetConnections() {
		if (getComment().getDomain() != null
				&& getComment().getDomain().isCollapsed()) {
			return Collections.EMPTY_LIST;
		} else {
			return super.getModelTargetConnections();
		}
	}

}
